package cn.schoolwow.quickdao.test.module.business.user.ddl.testcase;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickdao.test.module.business.user.dml.user.testcase.AddUserTestCaseFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class BackupTableTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(new AddUserTestCaseFlow());
        backupTable(flowContext);
    }

    @Override
    public String name() {
        return "备份表测试用例";
    }

    private void backupTable(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);

        Entity entity = dao.getEntity(User.class);
        dao.backupTable(entity.tableName, entity.tableName+"_backup");

        Assert.assertTrue(dao.hasTable(entity.tableName+"_backup"));
        long userCount = dao.query(User.class).execute().count();
        long userBackupCount = dao.query(entity.tableName+"_backup").execute().count();
        Assert.assertEquals(userCount, userBackupCount);

        dao.dropTable(entity.tableName+"_backup");
    }
}
